layui.use(["table", "layer", "form"], function() {
	var table = layui.table;
	var $ = layui.jquery;
	var layer = layui.layer;
	var form = layui.form;
	$("#searchBtn").on("click", function() {
		table.render({
			elem: "#stuTable",
			url: "/stu/list",
			page: true,
			limit: 5,
			limits: [5, 10, 15, 20],
			where: { name: $("#searchName").val(),status:$("#searchStatus").val() },
			cols: [[
				{title: "序号",type:'numbers',width: '5%'},
				{ title: '姓名', field: 'name', width: '15%', align: "center" },
				{ title: '学校', field: 'school', width: '20%', align: "center" },
				{
					title: '性别', width: '10%', templet: function(d) {
						return d.gender === "1" ? "男" : "女";
					}, align: "center"
				},
				{ title: '年龄', field: 'age', width: '15%', align: "center" },
				{ title: '状态', templet:function(d){
					if(d.status=='0'){
						return "有意向";
					}else if (d.status == "1"){
						return "已签合同";
					}else if (d.status == "2"){
						return "已进班";
					}else if (d.status == "3"){
						return "已就业离校";
					}
					}, width: '15%', align: "center" },
				{ title: '操作', width: '20%', align: "center", toolbar: "#operateTemplate" }
			]]
		});
		//数据表操作事件
		table.on('tool(stuTable1)', function(obj) {
			//点击删除按钮事件
			if (obj.event == "del") {
				layer.confirm("你确定要删除该学生吗？", function() {
					$.post("/stu/del?id=" + obj.data.id, function() {
						layer.closeAll();
						$("#searchBtn").click();
					});
				}, function() { });
			}
			//点击编辑按钮事件
			if (obj.event == "edit") {
				$.get("/stu/" + obj.data.id, function(ret) {
					layer.open({
						type: 1,
						title: "编辑学生",
						content: $("#userFormTemplate").html(),
						area: ['500px', '400px']
					});
					form.val("userForm", ret);
				});
			}
			//点击签合同按钮事件
			if(obj.event == "contract"){
				$.get("/dict/list?category=PAYTYPE",function(dicts){
					layer.open({
						type:1,
						title:'签订合同',
						content:$("#contractTemplate").html(),
						area:['500px','400px']
					});
					$("#stuId").val(obj.data.id);
					var options = "";
					$.each(dicts,function(i,v){
						options += "<option value = '"+v.id+"'>"+v.name+"</option>";
					});
					$("#payType").html(options);
					form.render();
					$("#saveContractBtn").on("click",function(){
						$.post("/stu/saveContract", form.val("contractForm"), function() {
							layer.closeAll();
							$("#searchBtn").click();
						});
					});
				});
			}
			//点击办理进班
			if(obj.event=="clazz"){
				$.get("/clazz/list",function(ret){
					var options = "";
					$.each(ret,function(i,v){
						options += "<option value = '"+v.id+"'>"+v.name+"</option>";
					});
					layer.open({
						type:1,
						title:'选择班级',
						content:$("#clazzTemplate").html(),
						area:['400px','300px']
					});
					$("#clazzId").html(options);
					form.render();
					$("#saveClazzBtn").on("click",function(){
						$.post("/stu/saveClazz",{clazzId:$("#clazzId").val(),stuId:obj.data.id},function(){
							layer.closeAll();
							$("#searchBtn").click();
						});
					});
				});
			}
			//点击的邀请按钮
			if(obj.event == "invite"){
				$.get("/stu/inviteCode?stuId=" + obj.data.id,function(code){
					console.log(code);
				});
			}
		});
	}).click();
	//给新增按钮绑定点击事件
	$("#addBtn").on("click", function() {
		layer.open({
			type: 1,
			title: "新增学生",
			content: $("#userFormTemplate").html(),
			area: ['500px', '400px']
		});
		form.render();
	});
	//弹出窗口中保存按钮绑定事件
	$("body").on("click", "#saveBtn", function() {
		$.post("/stu/save", form.val("userForm"), function() {
			layer.closeAll();
			$("#searchBtn").click();
		});
	});

});